package com.keyboardr.gamerater;

import net.londatiga.android.ActionItem;
import net.londatiga.android.QuickAction;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class GroupsFragment extends ListFragment implements
		OnItemLongClickListener {

	private ActionMode mCurrentActionMode;

	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setHasOptionsMenu(true);
	}

	@Override
	public void onStart() {
		super.onStart();
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		getListView().setOnItemLongClickListener(this);

		if (RaterActivity.isHolo) {
			registerForContextMenu(getListView());
			setListAdapter(new ArrayAdapter<Group>(getActivity(),
					android.R.layout.simple_list_item_activated_1,
					((GroupContainer) getActivity()).getGroupList()));
		} else {
			setListAdapter(new ArrayAdapter<Group>(getActivity(),
					android.R.layout.simple_list_item_1,
					((GroupContainer) getActivity()).getGroupList()));
		}
		updateGroupSelection();
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		((GroupContainer) getActivity()).selectGroup(position);
	}

	public void updateGroupSelection() {
		getListView().setItemChecked(
				((GroupContainer) getActivity()).getSelectedGroupPosition(),
				true);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.groups, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.newGroup:
			final Dialog dialog = new Dialog(getActivity());
			dialog.setContentView(R.layout.new_group);
			dialog.setTitle(R.string.newGroup);

			Button okButton = (Button) dialog.findViewById(R.id.groupOKButton);
			okButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					((GroupContainer) getActivity()).addGroup(new Group(
							((TextView) dialog.findViewById(R.id.groupNameText))
									.getText(), ((Spinner) dialog
									.findViewById(R.id.ruleSetSpinner))
									.getSelectedItemPosition() == 0));
					dialog.dismiss();
				}

			});
			Button cancelButton = (Button) dialog
					.findViewById(R.id.groupCancelButton);
			cancelButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.cancel();
				}
			});
			dialog.getWindow().setLayout(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT);
			dialog.show();
			return true;
		}
		return false;
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		if (RaterActivity.isHolo) {
			final int pos = position;
			if (mCurrentActionMode != null) {
				return false;
			}

			mCurrentActionMode = getActivity().startActionMode(
					new ActionMode.Callback() {

						@Override
						public boolean onCreateActionMode(ActionMode mode,
								Menu menu) {
							mode.setTitle(((GroupContainer) getActivity())
									.getGroupList().get(pos).toString());
							MenuInflater inflater = mode.getMenuInflater();
							inflater.inflate(R.menu.group_context, menu);
							return true;
						}

						@Override
						public boolean onPrepareActionMode(
								ActionMode paramActionMode, Menu paramMenu) {
							return false;
						}

						@Override
						public void onDestroyActionMode(
								ActionMode paramActionMode) {
							mCurrentActionMode = null;
						}

						@Override
						public boolean onActionItemClicked(ActionMode mode,
								MenuItem paramItem) {
							final MenuItem item = paramItem;
							switch (item.getItemId()) {
							case R.id.deleteGroup:
								mode.finish();
								AlertDialog.Builder builder = new AlertDialog.Builder(
										getActivity());
								builder.setMessage(
										"Are you sure you want to delete this group?")
										.setCancelable(false)
										.setPositiveButton(
												"Yes",
												new DialogInterface.OnClickListener() {
													public void onClick(
															DialogInterface dialog,
															int id) {
														((GroupContainer) getActivity())
																.removeGroup(pos);
													}
												})
										.setNegativeButton(
												"No",
												new DialogInterface.OnClickListener() {
													@Override
													public void onClick(
															DialogInterface dialog,
															int which) {
														dialog.cancel();
													}
												});
								Dialog deleter = builder.create();
								deleter.show();
								break;
							case R.id.selectGroup:
								mode.finish();
								((GroupContainer) getActivity())
										.selectGroup(pos);
							}
							return false;
						}
					});
			return true;
		} else {
			final int pos = position;
			final QuickAction quickAction = new QuickAction(view);
			ActionItem deleteAction = new ActionItem(getResources()
					.getDrawable(android.R.drawable.ic_menu_delete));
			deleteAction.setTitle(getResources()
					.getString(R.string.deleteGroup));
			deleteAction.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							getActivity());
					builder.setMessage(
							"Are you sure you want to delete this group?")
							.setCancelable(false)
							.setPositiveButton("Yes",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											((GroupContainer) getActivity())
													.removeGroup(pos);
										}
									})
							.setNegativeButton("No",
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.cancel();
										}
									});
					Dialog deleter = builder.create();
					quickAction.dismiss();
					deleter.show();

				}
			});
			quickAction.addActionItem(deleteAction);
			ActionItem selectAction = new ActionItem(getResources()
					.getDrawable(android.R.drawable.ic_menu_set_as));
			selectAction.setTitle(getResources()
					.getString(R.string.selectGroup));
			selectAction.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					quickAction.dismiss();
					((GroupContainer) getActivity()).selectGroup(pos);
				}
			});
			quickAction.addActionItem(selectAction);
			quickAction.show();
			return true;
		}
	}

	@Override
	public boolean onContextItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
		case R.id.deleteGroup:
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			builder.setMessage("Are you sure you want to delete this group?")
					.setCancelable(false)
					.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									((GroupContainer) getActivity())
											.removeGroup(((AdapterView.AdapterContextMenuInfo) item
													.getMenuInfo()).position);
								}
							})
					.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.cancel();
								}
							});
			Dialog deleter = builder.create();
			deleter.show();
			break;
		case R.id.selectGroup:
			((GroupContainer) getActivity())
					.selectGroup(((AdapterView.AdapterContextMenuInfo) item
							.getMenuInfo()).position);
		}
		return false;
	}
}
